//
//  ZYXMyViewController.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/2.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXMyViewController.h"
#import "ZYXSettingViewController.h"
#import "ZYXMYCell.h"
#import "ZYXMyHeaderView.h"
#import "ZYXMineCenterViewController.h"
#import "ZYXMessageVC.h"

#import "ZYXMyCollectVC.h"
#import "ZYXZYXMyOrderVC.h"
#import "ZYXLeaveMsgVC.h"
#import "ZYXAboutUsVC.h"
#import "ZYXBuyVC.h"
#import "ZYXHomeModel.h"
#import "ZYXMyModel.h"
@interface ZYXMyViewController ()

@property (strong,nonatomic) NSMutableArray *dataArr;
@property (strong,nonatomic) ZYXMyHeaderView *myHeader;

@property (strong,nonatomic) ZYXMyModel *model;

@end

@implementation ZYXMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
#pragma mark MainUI
-(void)setupViews{
//    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.frame = CGRectMake(0 , -StatusBar_Height, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-TabBar_Height+StatusBar_Height);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXMYCell" bundle:nil] forCellReuseIdentifier:@"ZYXMYCell"];
    self.myHeader = [[NSBundle mainBundle]loadNibNamed:@"ZYXMyHeaderView" owner:nil options:nil].lastObject;
    self.myHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
    [self.myHeader.iconBtn addTarget:self action:@selector(clickIcon) forControlEvents:UIControlEventTouchUpInside];
    [self.myHeader.msgButton addTarget:self action:@selector(clickMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.myHeader.setButton addTarget:self action:@selector(clickSetUp) forControlEvents:UIControlEventTouchUpInside];
    [self.myHeader.payOrVipButton addTarget:self action:@selector(clickPayOrVip) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = self.myHeader;
    
    [self.view addSubview:self.tableView];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickCeshi) name:@"1111111" object:nil];
    
}
-(void)loadData{
    NSString *strUid = UID;
    if (strUid.length >0) {
        [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_WDMyIndex) parmas:@{@"user_id":UID} successBloock:^(NSDictionary *responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"] isEqualToNumber:@200]) {
                
                self.model = [ZYXMyModel mj_objectWithKeyValues:responseObject[@"data"]];
                self.myHeader.model = self.model;
                
            }else{
                [JKToast showWithText:responseObject[@"message"]];
            }
            
            
        } failureBlock:^(NSError *error) {
            
        }];
    }
  
    
}
-(void)clickCeshi{
    NSLog(@"购买记录");
    ZYXZYXMyOrderVC *vc = [[ZYXZYXMyOrderVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark ZYXMyHeaderView
-(void)clickIcon{
    NSLog(@"点击头像");
    ZYXMineCenterViewController *ctrl = [[ZYXMineCenterViewController alloc] init];
    [self.navigationController pushViewController:ctrl animated:YES];
}
-(void)clickMessage{
     NSLog(@"点击消息");
    ZYXMessageVC *vc = [[ZYXMessageVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)clickSetUp{
     NSLog(@"点击设置");
    ZYXSettingViewController *ctrl = [[ZYXSettingViewController alloc] init];
    [self.navigationController pushViewController:ctrl animated:YES];
}
-(void)clickPayOrVip{
     NSLog(@"点击续费或付款");
    
    ZYXBuyVC *vc= [[ZYXBuyVC alloc]init];
    vc.typeStr = @"2";
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYXMYCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXMYCell"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 51;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        NSLog(@"我的收藏");
        ZYXMyCollectVC *vc =[[ZYXMyCollectVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }if (indexPath.row == 1) {
        NSLog(@"购买记录");
        ZYXZYXMyOrderVC *vc = [[ZYXZYXMyOrderVC alloc]init];
         [self.navigationController pushViewController:vc animated:YES];
         return;
    }if (indexPath.row == 2) {
        NSLog(@"一键反馈");
        ZYXLeaveMsgVC *vc = [[ZYXLeaveMsgVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
         return;
    }if (indexPath.row == 3) {
        NSLog(@"给我评分,,,,,,,跳转到APP评分");
        //1、cn代表中国。在Safari浏览器打开链接是中文介绍
        NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@&pageNumber=0&sortOrdering=2&mt=8", @"1448664574"];
        //打开链接地址
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];

         return;
    }else{
        NSLog(@"关于我们");
        ZYXAboutUsVC *vc= [[ZYXAboutUsVC alloc]init];
         [self.navigationController pushViewController:vc animated:YES];
        
         return;
    }
}
#pragma mark 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        ZYXHomeOneModel *model0= [[ZYXHomeOneModel alloc]initWitImgStr:@"shoucang" title:@"我的收藏"];
        ZYXHomeOneModel *model1= [[ZYXHomeOneModel alloc]initWitImgStr:@"goumaijilu" title:@"购买记录"];
        ZYXHomeOneModel *model2= [[ZYXHomeOneModel alloc]initWitImgStr:@"yijianfankui" title:@"一键反馈"];
        ZYXHomeOneModel *model3= [[ZYXHomeOneModel alloc]initWitImgStr:@"pingfen" title:@"给我评分"];
        ZYXHomeOneModel *model4= [[ZYXHomeOneModel alloc]initWitImgStr:@"guanyuwomen" title:@"关于我们"];
       
        _dataArr = [NSMutableArray arrayWithObjects:model0,model1,model2,model3,model4, nil];
        
    }
    return _dataArr;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    if ([UID integerValue] >0) {
        [self loadData];
    }else{
          [ZYXLoginViewController setupLogin];
    }
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
      self.navigationController.navigationBar.hidden = NO;
}

@end
